
package com.hlkj.pay.app.common.ext.tencent;

import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.OssExtPt;
import com.hlkj.pay.app.common.ext.CommonExtConstant;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.dto.common.StorageConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 腾讯云存储
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = CommonExtConstant.CHANNEL_TENCENT)
public class TencentOssExtPt implements OssExtPt {

    /**
     * 鉴权客户端
     *
     * @author fzr
     * @return String
     */
    public COSClient cosClient(StorageConfig storageConfig) {
        String secretId = storageConfig.getAccessKey();
        String secretKey = storageConfig.getSecretKey();
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(storageConfig.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        return new COSClient(cred, clientConfig);
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     *            文件对象
     * @param key
     *            文件名称 20220331/11.png
     */
    public String upload(MultipartFile multipartFile, String key, StorageConfig storageConfig) throws IOException {
        try {
            byte[] data = multipartFile.getBytes();
            InputStream inputStream = new ByteArrayInputStream(data);
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(multipartFile.getSize());
            PutObjectRequest putObjectRequest = new PutObjectRequest(storageConfig.getRegion(), key, inputStream, objectMetadata);
            this.cosClient(storageConfig).putObject(putObjectRequest);
            return storageConfig.getDomain() + key;
        }
        catch (CosClientException e) {
            throw new IOException(e.getMessage());
        }
        catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }

    @Override
    public String upload(MultipartFile multipartFile, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        return upload(multipartFile, ossEnum.getFolder() + "/" + key, storageConfig);
    }

    @Override
    public String upload(byte[] fileArray, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        try {
            byte[] data = fileArray;
            key=ossEnum.getFolder() + "/" + key;
            InputStream inputStream = new ByteArrayInputStream(data);
            ObjectMetadata objectMetadata = new ObjectMetadata();
            // objectMetadata.setContentLength(multipartFile.getSize());
            PutObjectRequest putObjectRequest = new PutObjectRequest(storageConfig.getRegion(), key, inputStream, objectMetadata);
            this.cosClient(storageConfig).putObject(putObjectRequest);
            return storageConfig.getDomain() + key;
        }
        catch (CosClientException e) {
            throw new IOException(e.getMessage());
        }
        catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }

    @Override
    public String uploadZip(List<String> fileList, CommonEnum.OssEnum ossEnum, String key, StorageConfig storageConfig) throws IOException {
        return "";
    }
}
